import {
    nextTick,
    msg,
    open,
    setPageNumber,
    confirm
} from '../../common/utils.js';
import Vue from 'vue';
import {
    serverPath,
    json,
    currentPath
} from '../../common/config.js';
import page from '../../../components/page.vue';
import ActivityService from '../services/activityService.js';

window.Module = function () {
    let activityService = new ActivityService();
    return new Vue({
        el: '#indexController',
        data: {
            pager: {
                pageIndex: 1,
                pageSize: 10,
                pageCount: 0,
                totalCount: 0,
                pageNumber: []
            },
            list: [],
            searchCondition: {
                name: ''
            }
        },
        methods: {
            init: async function () {
                await this.loadData();
            },
            showAddPage:async function () { 
                let options = {
                    title: "新增",
                    content: `${currentPath}dialog.html#/ActivityPage/AddPage`,
                    maxmin: false,
                    area: ['80%', '70%']
                };
                await open(options);
                await this.loadData();
            },
            pageChange: async function (pageIndex, e) {
                this.pager.pageIndex = pageIndex;
                await this.loadData();
            },
            loadData: async function () {
                let res = await activityService.getActivity(this.pager.pageIndex, this.pager.pageSize);
                if (!res.type) {
                    return;
                }
                else if (json === res.type) {
                    var response = res.body;
                    if ('Success' === response.result) {
                        this.list = response.data.data;
                        for (let item in response.data) {
                            if ('data' !== item) {
                                this.pager[item] = res.body.data[item];
                            }
                        }
                        setPageNumber(this.pager);
                    }
                    else {
                        await msg(response.message);
                    }
                }
            },
            setValid: async function (item) {
                if (!item)
                    return;
                await confirm(`确定要修改有效值(活动名称:${item.title})？`);
                let res = await activityService.updateActivityValid(item.id);
                if (json === res.type) {
                    var response = res.body;
                    if ('Success' === response.result) {
                        await this.loadData();
                    }
                    else {
                        await msg(response.message);
                    }
                }

            },
            deleteActivity: async function (item) {
                if (!item)
                    return;
                await confirm(`确定要删除活动(活动名称:${item.title})？`);
                let res = await activityService.deleteActivity(item.id);
                if (json === res.type) {
                    var response = res.body;
                    if ('Success' === response.result) {
                        await this.loadData();
                    }
                    else {
                        await msg(response.message);
                    }
                }
            },
            detailActivity: async function (item) {
                let options = {
                    title: "活动详情",
                    content: `${currentPath}dialog.html#/ActivityPage/DetailPage?id=${item.id}`,
                    maxmin: false,
                    area: ['80%', '70%']
                };
                await open(options);
            },
            editActivity: async function (item) {
                let options = {
                    title: "编辑活动",
                    content: `${currentPath}dialog.html#/ActivityPage/EditPage?id=${item.id}`,
                    maxmin: false,
                    area: ['80%', '70%']
                };
                await open(options);
                await this.loadData();
            }
        },
        components: {
            "page": page
        }
    });
};